package com.will.txj.common.utils;

import org.springframework.util.StringUtils;
import org.springframework.web.util.UriBuilder;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: wen-yi;
 * @date: 2021/12/4 17:15;
 * @Description:
 */
public class URLUtil {
    public static final String URL_SPLIT = "[?]";
    public static final String PARAM_SPLIT = "&";
    public static final String KEY_VALUE_SPLIT = "=";
    public static Map<String,String> getUrlParamMap(String urlParam){
        if(StringUtils.isEmpty(urlParam)){
            return null;
        } else{
            if (urlParam.contains("?")) {
                String[] split = urlParam.split(URL_SPLIT);
                urlParam = split[split.length-1];
            }
            Map<String,String> resultMap = new HashMap<>();
            String[] urlParams = urlParam.split(PARAM_SPLIT);
            for (String param : urlParams){
                String[] keyValue = param.split(KEY_VALUE_SPLIT);
                if(keyValue.length == 2){
                    resultMap.put(keyValue[0],keyValue[1]);
                }
            }
            return resultMap;
        }
    }

    public static void main(String[] args) {
        String url = "?das?a=5&b=6";
        Map<String, String> urlParamMap = getUrlParamMap(url);
        System.out.println(urlParamMap);
    }
}
